#include <a_samp>
#include <file>

#include <cstring>




stock FindLODModel(const listname[], modelid)
{
	new File:fp;

	fp = fopen(listname, filemode:io_read);
	if(!fp) return modelid;

	new line[256], oid = 0;
	new tmp[64];
	new tmp2 = strrchr(listname, '/');
	new listpath[64];
	if(tmp2 != -1)
		strmid(listpath, listname, 0, tmp2);

	while(fread(fp, line, sizeof(line), false) > 0)
	{
		if(line[0] == '#')
			continue;

		if(tmp2 != -1)
		{
			format(line, sizeof(line), "%s/%s", listpath, line);
			line[strrchr(line, '\r')] = EOS;
			//print(line);
		}

		if(fexist(line))
		{
			if(FindObjectModelNameInFile(line, modelid, tmp) != 0)
			{
				fclose(fp);
				//printf("found %s in %s for %d", tmp, line, modelid);
				tmp[0] = 'L';
				tmp[1] = 'O';
				tmp[2] = 'D';

				if((oid = FindObjectModelIDInFile(line, tmp)) != 0)
				{
					//printf("found %d in %s for %s", oid, line, tmp);
					return oid;
				}
				return modelid;
			}
		}

	}
	fclose(fp);

	return 0;

}





stock FindObjectModelName(const prefix[], const filename[], modelid, dest[])
{
	new File:fp;

	fp = fopen(filename, io:io_read);
	if(!fp) return 0;

	new line[256], oid = 0;

	while(fread(fp, line, sizeof(line), false) > 0)
	{
		if(line[0] == '#')
			continue;

		format(line, sizeof(line), "%s/%s", prefix, line);

		if(fexist(line))
		{
			if(FindObjectModelNameInFile(line, modelid, dest) != 0)
			{
				fclose(fp);
				return oid;
			}
		}

	}
	fclose(fp);

	return 0;

}


stock FindObjectModelNameInFile(const filename[], modelid, dest[])
{
	new File:fp;

	fp = fopen(filename, filemode:io_read);
	if(!fp) return 0;

	new line[256], oid;
	new tmp[64], idx;

	while(fread(fp, line, sizeof(line), false) > 0)
	{
		//idx = 0;
		if(line[0] == '#')
			continue;

		idx = token_by_delim(line, tmp, ',', 0);

		oid = strval(tmp);
		if(oid == modelid)
		{
			idx = token_by_delim(line, dest, ',', idx + 1);
			if(idx == -1)  //	:(
				continue;
			fclose(fp);
			return modelid; //	:)
		}


	}
	fclose(fp);

	return 0;
}


stock FindObjectModelIDInFile(const filename[], modelname[])
{
	new File:fp;

	fp = fopen(filename, filemode:io_read);
	if(!fp) return 0;

	new line[256], oid;
	new tmp[64], idx;

	while(fread(fp, line, sizeof(line), false) > 0)
	{
		//idx = 0;
		if(line[0] == '#')
			continue;

		idx = token_by_delim(line, tmp, ',', 0);

		oid = strval(tmp);
		if(oid < 1) continue;

		idx = token_by_delim(line, tmp, ',', idx+1);

		if(strcmp(tmp, modelname, true) == 0)
		{
			fclose(fp);
			return oid; //	:)
		}


	}
	fclose(fp);

	return 0;
}



static stock token_by_delim(const string[], return_str[], delim, start_index)
{
	new x=0;
	while(string[start_index] == ' ' || string[start_index] == '\t')
		start_index++;
	while(string[start_index] != EOS && string[start_index] != delim && string[start_index] != '#')
	{
		return_str[x] = string[start_index];
		x++;
		start_index++;
	}
	return_str[x] = EOS;
	if(string[start_index] == EOS) start_index = (-1);
	return start_index;
}

